//
//  LanguageManger.m
//  AppData
//
//  Created by CPX on 2018/5/23.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "LanguageManager.h"
#import "AppDataManager.h"
#import <UIKit/UIKit.h>
#import <objc/message.h>

NSString *const AppLanguageChangedNotification = @"app_language_changed_noti";
NSString *const QuranLanguageChangedNotification = @"quran_language_changed_noti";

#define QuranLang @"quran_language"
#define TransLang @"trans_language"
#define PhoneLang @"phone_language"
#define MP3Lang @"mp3_language"
#define AppLang @"app_language"
#define AppLangName @"app_lan_name"

/**
 语言管理类
 @author yzl
 */
@interface LanguageManager()

@property (nonatomic,strong) NSUserDefaults *defaults;
@property (nonatomic,strong) NSBundle *bundle;

@end

@implementation LanguageManager

+ (instancetype)defaultManager{
    static LanguageManager *mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [self new];
        [mgr _init];
    });
    return mgr;
}

- (void)_init{
    
    self.defaults = [NSUserDefaults standardUserDefaults];
    
//    self.appLanguage = [self.defaults stringForKey:AppLang];
//    self.appLanName = [self.defaults stringForKey:AppLangName];
    self.localeLanguage = [NSLocale preferredLanguages][0];
    if (!self.appLanguage) {
        NSRange r = [self.localeLanguage rangeOfString:@"-"];
        if (r.location != NSNotFound) {
            self.appLanguage = [self.localeLanguage substringToIndex:r.location];
        }
    }
    
    self.quranLanguage = [self.defaults stringForKey:QuranLang];
    if (!self.quranLanguage) self.quranLanguage = self.localeLanguage;
    
    self.transLanguage = [self.defaults stringForKey:TransLang];
    if (!self.transLanguage) self.transLanguage = @"EN_SAHEEH";
    
    self.phoneLanguage = [self.defaults stringForKey:PhoneLang];
    if (!self.phoneLanguage) self.phoneLanguage = @"EN_TRANSLITERATION";
    
    self.mp3Language = [self.defaults stringForKey:MP3Lang];
    if (!self.mp3Language) self.mp3Language = @"AL-GHAMDI";
    
    [self resetLocaleLan:self.appLanguage];
    
    if ([self.appLanguage isEqualToString:@"ar"]) {
        if (@available(iOS 9.0, *)) {
            [AppDataManager sharedManager].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
            [UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
        } else {
            // Fallback on earlier versions
            
        }
    }
    else{
        [UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
        [AppDataManager sharedManager].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    }
}

- (NSString*)getStringForKey:(NSString *)key{
    return NSLocalizedStringFromTableInBundle(key, @"Localizable", self.bundle, @"");
}

- (void)saveLanguagesWithTrans:(NSString *)trans pho:(NSString *)pho mp3:(NSString *)mp3{
    self.transLanguage = trans;
    self.phoneLanguage = pho;
    self.mp3Language = mp3;
    [self.defaults setObject:self.transLanguage forKey:TransLang];
    [self.defaults setObject:self.phoneLanguage forKey:PhoneLang];
    [self.defaults setObject:self.mp3Language forKey:MP3Lang];
}

- (void)resetLocaleLan:(NSString *)localLan{
    
    self.appLanguage = localLan;
    
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"lans" ofType:@"plist"];
    NSArray *a = [NSArray arrayWithContentsOfFile:path1];
    BOOL find = NO;
    for (NSDictionary *d in a) {
        if ([d[@"code"] isEqualToString:self.appLanguage]) {
            self.appLanName = d[@"title"];
            find = YES;
            break;
        }
    }
    if (!find) {
        self.appLanguage = @"en";
        self.appLanName = @"English";
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"lan" ofType:@"bundle"];
    self.bundle = [NSBundle bundleWithPath:path];
    path = [self.bundle pathForResource:self.appLanguage ofType:@"lproj"];
    NSString *p2 = nil;
    p2 = path.copy;
    if (path == nil) {
        self.appLanguage = @"en";
        
        
        
        for (NSDictionary *d in a) {
            if ([d[@"code"] isEqualToString:self.appLanguage]) {
                self.appLanName = d[@"title"];
                break;
            }
        }
        path = [self.bundle pathForResource:@"en" ofType:@"lproj"];
        
        p2 = path.copy;
    }
    self.bundle = [NSBundle bundleWithPath:p2];
    
    [self.defaults setObject:self.appLanguage forKey:AppLang];
    [self.defaults setObject:self.appLanName forKey:AppLangName];
    
    Class c = NSClassFromString(@"BaseApi");
    SEL s1 = NSSelectorFromString(@"standard");
    SEL s2 = NSSelectorFromString(@"setLanguage:");
    if (c) {
        id ins = [c performSelector:s1];
        [ins performSelector:s2 withObject:self.appLanguage];
        
    }
}

@end
